import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import('../components/Login')
const Home = () => import('../components/Home')
const Welcome = () => import('../components/Welcome')
const Users = () => import('../components/users/Users')
const Rights = () => import('../components/power/Rights')
const Roles = () => import('../components/power/Roles')
const Categories=()=>import('../components/goods/Categories')
const Params=()=>import('../components/goods/Params')
const Goods=()=>import('../components/goods/Goods')
const Add=()=>import('../components/goods/Add')
const Order=()=>import('../components/order/Order')
const Report=()=>import('../components/report/Report')
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      { 
        path: '/rights', 
        component: Rights 
      },
      { 
        path: '/roles', 
        component: Roles 
      },
      { 
        path: '/categories', 
        component: Categories 
      },
      { 
        path: '/params', 
        component: Params 
      },
      { 
        path: '/goods', 
        component: Goods ,
      },
      { 
        path: '/goods/add', 
        component: Add 
      },
      { 
        path: '/orders', 
        component: Order 
      },
      { 
        path: '/reports', 
        component: Report 
      },
    ]
  },
  
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // form 代表从哪个路径跳转过来
  // next 是一个函数 表示放行 next() 放行 next('/xxxx')强制跳转
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) {
    return next('/login')
  } else {
    return next()
  }
})

export default router
